package com.example.dryyzx.dao;

import com.example.dryyzx.dao.AccountSqlProvider;
import com.example.dryyzx.entity.Account;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Repository;
import org.apache.ibatis.annotations.Mapper;


@Mapper
public interface AccountMapper {
    @Delete({
            "delete from account",
            "where account_number = #{accountNumber,jdbcType=VARCHAR}"
    })
    int deleteByPrimaryKey(String accountNumber);

    @Insert({
            "insert into account (account_number, password, ",
            "type, user_id, enable)",
            "values (#{accountNumber,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, ",
            "#{type,jdbcType=CHAR}, #{userId,jdbcType=INTEGER}, #{enable,jdbcType=BIT})"
    })
    int insert(Account record);

    @InsertProvider(type=AccountSqlProvider.class, method="insertSelective")
    int insertSelective(Account record);

    @Select({
            "select",
            "account_number, password, type, user_id, enable",
            "from account",
            "where account_number = #{accountNumber,jdbcType=VARCHAR}"
    })
    @Results({
            @Result(column="account_number", property="accountNumber", jdbcType=JdbcType.VARCHAR, id=true),
            @Result(column="password", property="password", jdbcType=JdbcType.VARCHAR),
            @Result(column="type", property="type", jdbcType=JdbcType.CHAR),
            @Result(column="user_id", property="userId", jdbcType=JdbcType.INTEGER),
            @Result(column="enable", property="enable", jdbcType=JdbcType.BIT)
    })
    Account selectByPrimaryKey(String accountNumber);

    @Select({
            "select",
            "account_number, password, type, user_id, enable",
            "from account",
            "where user_id = #{userId,jdbcType=VARCHAR} and enable=1"
    })
    @Results({
            @Result(column="account_number", property="accountNumber", jdbcType=JdbcType.VARCHAR, id=true),
            @Result(column="password", property="password", jdbcType=JdbcType.VARCHAR),
            @Result(column="type", property="type", jdbcType=JdbcType.CHAR),
            @Result(column="user_id", property="userId", jdbcType=JdbcType.INTEGER),
            @Result(column="enable", property="enable", jdbcType=JdbcType.BIT)
    })
    Account selectByUserId(Integer userId);

    @Select({
            "select",
            "account_number, type, user_id, enable",
            "from account",
            "where account_number = #{uname,jdbcType=VARCHAR} and password = #{pwd,jdbcType=VARCHAR} and enable=1"
    })
    @Results({
            @Result(column="account_number", property="accountNumber", jdbcType=JdbcType.VARCHAR, id=true),
            @Result(column="password", property="password", jdbcType=JdbcType.VARCHAR),
            @Result(column="type", property="type", jdbcType=JdbcType.CHAR),
            @Result(column="user_id", property="userId", jdbcType=JdbcType.INTEGER),
            @Result(column="enable", property="enable", jdbcType=JdbcType.BIT)
    })
    Account selectByUnameAndPwd(@Param("uname") String uname, @Param("pwd") String pwd);



    @UpdateProvider(type= AccountSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(Account record);

    @Update({
            "update account",
            "set password = #{password,jdbcType=VARCHAR},",
            "type = #{type,jdbcType=CHAR},",
            "user_id = #{userId,jdbcType=INTEGER},",
            "enable = #{enable,jdbcType=BIT}",
            "where account_number = #{accountNumber,jdbcType=VARCHAR}"
    })
    int updateByPrimaryKey(Account record);
}